﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Erebos.GameScreenFiles.WorldObjects
{
    class ExplosionObject : Object
    {
        private Vector3 _position;
        private Vector3 _velocity;

        private ParticleSystem _explosionParticles;
        private ParticleSystem _explosionSmokeParticles;

        private Random _random = new Random();

        public ExplosionObject(Vector3 position)
        {
            _position = position;

            _velocity.X = (float)(_random.NextDouble() - 0.5) * 60;
            _velocity.Y = (float)(_random.NextDouble() + 0.5) * 40;
            _velocity.Z = (float)(_random.NextDouble() - 0.5) * 60;

            _explosionParticles = new ExplosionParticleSystem(MainGame.Game, MainGame.GetContentManager());
            _explosionSmokeParticles = new ExplosionSmokeParticleSystem(MainGame.Game, MainGame.GetContentManager());

            MainGame.Game.Components.Add(_explosionParticles);
            MainGame.Game.Components.Add(_explosionSmokeParticles);
        }

        public override void Update(GameTime gameTime)
        {
            _explosionParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));
            _explosionSmokeParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));

            if (!Free)
            {
                for (int i = 0; i < 30; i++)
                    _explosionParticles.AddParticle(new Vector3(_position.X, _position.Y, -_position.Z), _velocity);

                for (int i = 0; i < 50; i++)
                    _explosionSmokeParticles.AddParticle(new Vector3(_position.X, _position.Y, -_position.Z), _velocity);

                base.Free = true;

                base.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _explosionParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            _explosionSmokeParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            base.Draw(gameTime);
        }
    }
}
